<?php
/**
 * Description of Proveedor
 *
 * @author Nelifer
 */
include_once('BDManager.class.php');

class Proveedor {
    var $con;
    function Proveedor(){
        $this->con=new BDManager();
    }

    function insertar($campos, $mapIds){
        if($this->con->conectar()==true){
            $sql = "INSERT INTO proveedores (pro_cirif, pro_razonsocial, pro_estatus, pro_tipopersona, pro_direccion, pro_email, pro_sitioweb, pro_tlfoficina, ".
                   "pro_tlfmovil, pro_tlfadicional, pro_fechacreacion, pro_horacreacion, usu_id ) ".
                   "VALUES ('".$campos[0]."','".$campos[1]."','".$campos[2]."','".$campos[3]."','".$campos[4]."','".$campos[5]."','".$campos[6]."','".
                               $campos[7]."','".$campos[8]."','".$campos[9]."','".$campos[10]."','".$campos[11]."', '".$campos[12]."')";
            $res1 = mysql_query($sql);
            
            if(count($mapIds) > 0){
                $BD = new BDManager();
                $id = $BD->get_current_insert_id("proveedores");

                $sql2 = "INSERT INTO proveedoresmateriasprimas (map_id, pro_id) VALUES ";
                for ($i = 0, $count = count($mapIds); $i < $count; $i++) {
                    $sql2 = $sql2 . "('".$mapIds[$i]."', '".$id."'), ";
                }
                $sql2 = substr($sql2, 0, (strlen($sql2)-2) )  . ";";

                $res2 = mysql_query($sql2);
            }else{
                $res2 = true;
            }
            
            return ($res1 && $res2)? true : false; 
        }
    }

    function actualizar($campos, $mapIds, $id){
        if($this->con->conectar()==true){
            $sql = "UPDATE proveedores SET pro_cirif = '".$campos[0]."', pro_razonsocial = '".$campos[1]."', pro_tipopersona = '".$campos[2]."', ".
                   "pro_direccion = '".$campos[3]."', pro_email = '".$campos[4]."', pro_tlfoficina = '".$campos[5]."', pro_tlfmovil = '".$campos[6]."', " .
                   "pro_tlfadicional = '".$campos[7]."' WHERE pro_id = ".$id;
            $res1 = mysql_query($sql);
            
            $sqlDelete = "DELETE FROM proveedoresmateriasprimas WHERE pro_id = $id ";            
            mysql_query($sqlDelete);
            
            if($mapIds!=null && count($mapIds) > 0){
                $sql2 = "INSERT INTO proveedoresmateriasprimas (map_id, pro_id) VALUES ";
                for ($i = 0, $count = count($mapIds); $i < $count; $i++) {
                    $sql2 = $sql2 . "('".$mapIds[$i]."', '".$id."'), ";
                }
                $sql2 = substr($sql2, 0, (strlen($sql2)-2) )  . ";";
                
                $res2 = mysql_query($sql2);
            }else{
                $res2 = true;
            }
            
            return ($res1 && $res2)? true : false; 
            
        }
    }

    function getConsulta($id){
        if($this->con->conectar()==true){
            return mysql_query("SELECT * FROM proveedores WHERE pro_id=".$id);
        }
    }

    function getConsultas(){
        if($this->con->conectar()==true){
            return mysql_query("SELECT pro_id, pre_cirif, pro_razonsocial FROM proveedores ORDER BY pro_id ASC");
        }
    }

    function eliminar($id){
        if($this->con->conectar()==true){
            //$sql2 = "DELETE FROM proveedoresmateriasprimas WHERE pro_id = $id ";            
            //echo "\nSQL2: ".$sql2;
            //$res2 = mysql_query($sql2);
            $sql = "DELETE FROM proveedores WHERE pro_id=".$id;
            //echo "\nSQL: ".$sql;
            $res1 = mysql_query($sql);
           
            
            return ($res1 /*&& $res2*/)? true : false;
        }
    }
    private function countRec($fname,$tname,$where) {
        $con = null;
        $sql = "SELECT count($fname) FROM $tname $where";
        if($this->con->conectar()==true){
            $con = $this->con->conectar();
            $result = mysql_query($sql);
        }
        while ($row = mysql_fetch_array($result)) {
            return $row[0];
        }
    }
    
    public function consultar_grid($page=null, $rp=null, $sortname=null, $sortorder=null, $query=null, $letter_pressed=null, $qtype=null){
        if (!$sortname) $sortname = 'name';
        if (!$sortorder) $sortorder = 'desc';
        if($query!=''){
             $where = "WHERE `".$qtype."` LIKE '%".$query."%' ";
        } else {
            $where ='';
        }
        if($letter_pressed!=''){
            $where = "WHERE `".$qtype."` LIKE '".$letter_pressed."%' ";	
        }
        if($letter_pressed=='#'){
            $where = "WHERE `".$qtype."` REGEXP '[[:digit:]]' ";
        }
        $sort = "ORDER BY $sortname $sortorder";

        if (!$page) $page = 1;
        if (!$rp) $rp = 10;
        $start = (($page-1) * $rp);
        $limit = "LIMIT $start, $rp";
        $sql = "SELECT pro.pro_id, pro.pro_cirif, pro.pro_razonsocial, vva.vva_desc pro_tipopersonaD, ". 
                      "pro.pro_direccion, CONCAT(usu.usu_nombre, ' '  ,usu.usu_apellido) usu_nombre ".
               "FROM proveedores pro ".
                    "INNER JOIN usuarios usu on usu.usu_id = pro.usu_id ".
                    "INNER JOIN valoresvariables vva on vva.vva_valor = pro.pro_tipopersona and vva.vva_cod = 'tpo_per' $where $sort $limit";
        //echo "SQL: ". $sql;
        $con = $this->con->conectar();
        $result = mysql_query($sql);
        //$this->con->close($con);
        $total = $this->countRec('pro_id','proveedores',$where);
        // Return JSON data
        $data = array();
        $data['page'] = $page;
        $data['total'] = $total;
        $data['rows'] = array();
        while ($row = mysql_fetch_assoc($result)) {            
            $data['rows'][] = array(
            'id' => $row['pro_id'],
            'cell' => array($row['pro_id'], $row['pro_cirif'], $row['pro_razonsocial'], $row['pro_tipopersonaD'], $row['pro_direccion'], $row['usu_nombre'] )
            );
        }
        echo json_encode($data);
    }
    
    public function getItemsProveedor(){
        if($this->con->conectar()==true){
            $con = $this->con->conectar();
            $param = trim($_GET['term']);

            $sql = "SELECT pro_id, pro_cirif, pro_razonsocial FROM proveedores where (pro_razonsocial like '%".$param ."%' OR '".$param."' = '' ) OR (pro_cirif like '%".$param ."%' OR '".$param."' = '' )";
            $result = mysql_query($sql);

            $data   = array();
            while ($row = mysql_fetch_assoc($result)) {
                $row_array['id'] = $row['pro_id'];
                $row_array['value'] = $row['pro_cirif'] . " / " .$row['pro_razonsocial'];
                $row_array['abbrev'] = $row['pro_razonsocial'];
                array_push($data,$row_array);

            }
        }
        echo json_encode($data);
        
    }
    
    public function getProveedor( $proId = null ){
        $result = $this->getConsulta($proId);

        $data   = array();
        while ($row = mysql_fetch_assoc($result)) {
            $row_array['id']     = $row['PRO_ID'];
            $row_array['cirif']  = $row['PRO_CIRIF'];
            $row_array['razonS'] = $row['PRO_RAZONSOCIAL'];
            $row_array['direcF'] = $row['PRO_DIRECCION'];
            $row_array['tlfOfi'] = $row['PRO_TLFOFICINA'];
            array_push($data,$row_array);

        }

        echo json_encode($data);
        
    }
    public function getArrayProveedor( $proId = null ){
        $result = $this->getConsulta($proId);

        $row_array   = array();
        while ($row = mysql_fetch_assoc($result)) {
            $row_array['id']     = $row['PRO_ID'];
            $row_array['cirif']  = $row['PRO_CIRIF'];
            $row_array['razonS'] = $row['PRO_RAZONSOCIAL'];
            $row_array['direcF'] = $row['PRO_DIRECCION'];
            $row_array['tlfOfi'] = $row['PRO_TLFOFICINA'];
            //array_push($data,$row_array);

        }
        return $row_array;
        
    }
    
    public function getOutProveedorMateriaP( $proId = null ){
        if($this->con->conectar()==true){
            if ($proId == 0) $todos = " OR 0 = 0 ";
            else $todos = " ";
            
            $sql =  "SELECT map.map_id, map.map_codigo, map.map_nombre " .
                    "FROM materiasprimas map " .
                    "WHERE map.map_id NOT IN (SELECT pma.map_id FROM proveedoresmateriasprimas pma WHERE pma.pro_id = $proId  or $proId = 0) $todos".  
                    "ORDER BY map.map_nombre ASC ";
            
            return mysql_query($sql);
        }        
    }
    public function getInProveedorMateriaP( $proId = null ){
        if($this->con->conectar()==true){
            if ($proId == 0) $todos = " OR 0 = 0 ";
            else $todos = " ";
            
            $sql =  "SELECT map.map_id, map.map_codigo, map.map_nombre " .
                    "FROM proveedoresmateriasprimas pma ".
                    "INNER JOIN materiasprimas map on map.map_id = pma.map_id ".
                    "WHERE pma.pro_id = $proId ORDER BY map.map_nombre ASC";
            
            return mysql_query($sql);
        }        
    }
}

?>
